package cn.diancan.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.Color;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.ColorService;
import cn.diancan.util.DateUtil;

@Component("colorService")
public class ColorServiceImpl implements ColorService{
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<Color> colorDao;
    

    @Override
    public boolean saveColor(String colorNumber, String colorName,String colorGroupNumber) throws OutMsgException,Exception {
	Color color = findColorByColorNumber(colorNumber);
	if(null!=color){
	    throw new OutMsgException("该颜色已存在");
	}
	if(null==colorName || "".equals(colorName.trim())){
	    //throw new OutMsgException("颜色名称不能为空");
	    colorName="";
	}
	colorName = colorName.trim();
	if(null==colorGroupNumber  || "".equals(colorGroupNumber.trim())){
	    colorGroupNumber ="";
	}
	colorGroupNumber = colorGroupNumber.trim();
	
	color =new Color();
	color.setColorGroupNumber(colorGroupNumber);
	color.setColorName(colorName);
	color.setColorNumber(colorNumber);
	color.setIsDelete(CommonType.Delete.NO);
	color.setCreateTime(DateUtil.currentDateTime());
	colorDao.save(color); 
	return true;
    }

    
    @Override
    public Color findColorByColorNumber(String colorNumber)throws OutMsgException {
	if(null==colorNumber || "".equals(colorNumber.trim())){
	    throw new OutMsgException("颜色编号不能为空");
	}
	colorNumber =colorNumber.trim();
	Color color = colorDao.get("FROM Color WHERE colorNumber=?", new Object[]{colorNumber});
	if(null!=color && color.getIsDelete().equals(CommonType.Delete.YES)){
	   throw new OutMsgException("该颜色已被删除");
	}
	
	return color;
    } 
    

    @Override
    public Color findColorById(Long id)throws OutMsgException {
	if(null==id  ){
	    throw new OutMsgException("参数错误");
	} 
	Color color = colorDao.get("FROM Color WHERE id=? AND isDelete=?", new Object[]{id,CommonType.Delete.NO});
	 
	return color;
    }


    @Override
    public Color findColorByColorName(String colorName) throws OutMsgException {
	if(null==colorName || "".equals(colorName.trim())){
	    throw new OutMsgException("颜色编号不能为空");
	}
	colorName =colorName.trim();
	Color color = colorDao.get("FROM Color WHERE colorName=?", new Object[]{colorName});
	if(null!=color && color.getIsDelete().equals(CommonType.Delete.YES)){
	   throw new OutMsgException("该颜色已被删除");
	}
	
	return color;
    }
    
    

}
